package 强化训练;

public class 井字棋 {
    public static void main(String[] args) {
        int[][] arr = {{1,0,-1},{1,0,-1},{1,-1,0}};
        System.out.println(checkWon(arr));
    }

    public static boolean checkWon(int[][] board) {
        // write code here
        int cols = board.length;//行
        int rols = board[0].length;//列
        //如果实在第一行，又除了第一个和最后一个需要判断斜线，其余的全都判断列是否成功
        for(int i = 0,j = 0; i <cols-1 && j < rols-1; i++,j++){//45°
            if(board[i][j] == board[i+1][j+1]){
                if(i == cols - 1 && j == rols - 1) {
                    return true;
                }
            }else {
                break;
            }
        }
        for(int i = cols - 1,j = 0; i >= 0 && j < rols-1; i--,j++){//135°
            if(board[i][j] == board[i-1][j+1]){
                if(i == 0 && j == rols - 1) {
                    return true;
                }
            }else {
                break;
            }
        }
        for(int i = 0 ;i < cols ;i ++){
            for(int j = 0;j < rols-1 ;j ++){
                if(board[i][j] == board[i][j+1]  && board[i][j] != 0){
                    if(j == rols - 1) return true;
                }else {
                    break;
                }
            }
        }
        for(int j = 0 ;j < rols;j++){
            for(int i = 0; i < cols - 1;i++){
                if(board[i][j] == board[i+1][j]  && board[i][j] != 0){
                    if(i + 1 == cols - 1) return true;
                }
            }
        }
        return false;
        //如果实在第二行及以下，只需要判断当前行的是否满足获胜情况
    }
}

